package com.example.personblog.utils;

import io.minio.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Component
public class MinioUtil {
    
    @Autowired
    private MinioClient minioClient;
    
    @Value("${minio.bucket.avatar}")
    private String avatarBucket;
    
    @Value("${minio.bucket.cover}")
    private String coverBucket;
    
    private static final int SEVEN_DAYS_IN_SECONDS = 7 * 24 * 60 * 60;  // 7天的秒数
    
    /**
     * 上传头像
     */
    public String uploadAvatar(MultipartFile file) {
        try {
            // 检查bucket是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(avatarBucket)
                    .build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(avatarBucket)
                        .build());
            }
            
            // 生成文件名
            String fileName = UUID.randomUUID().toString().replaceAll("-", "") 
                    + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            
            // 上传文件
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(avatarBucket)
                    .object(fileName)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build());
            
            // 生成访问URL
            String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .bucket(avatarBucket)
                    .object(fileName)
                    .method(Method.GET)
                    .expiry(SEVEN_DAYS_IN_SECONDS)  // 使用常量
                    .build());
            
            return url;
        } catch (Exception e) {
            throw new RuntimeException("上传头像失败", e);
        }
    }
    
    /**
     * 上传帖子封面
     */
    public String uploadCover(MultipartFile file) {
        try {
            // 检查bucket是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(coverBucket)
                    .build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(coverBucket)
                        .build());
            }
            
            // 生成文件名
            String fileName = UUID.randomUUID().toString().replaceAll("-", "") 
                    + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            
            // 上传文件
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(coverBucket)
                    .object(fileName)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build());
            
            // 生成访问URL
            String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .bucket(coverBucket)
                    .object(fileName)
                    .method(Method.GET)
                    .expiry(SEVEN_DAYS_IN_SECONDS)
                    .build());
            
            return url;
        } catch (Exception e) {
            throw new RuntimeException("上传封面失败", e);
        }
    }
} 